﻿using GalaSoft.MvvmLight;
using wp7cinema.Model;
using System.Collections.Generic;
using System.Net;
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Collections.ObjectModel;
using GalaSoft.MvvmLight.Command;
using wp7cinema.ExtendedMethod;
using Microsoft.Phone.Controls;
using System.Windows;
using System.Text.RegularExpressions;

namespace wp7cinema.ViewModel
{
    public class TheaterSearch2ViewModel : ViewModelBase
    {
        SubRegion selectedRegion;
        TheaterItem selectedTheater;

        #region 바인딩될 데이터들..
        private IEnumerable<Region> _region1;
        public IEnumerable<Region> Region1
        {
            get { return _region1; }
            set
            {
                _region1 = value;
                RaisePropertyChanged("Region1");
            }
        }

        private IEnumerable<SubRegion> _region2;
        public IEnumerable<SubRegion> Region2
        {
            get { return _region2; }
            set
            {
                _region2 = value;
                RaisePropertyChanged("Region2");
            }
        }

        private IEnumerable<TheaterItem> _theaters;
        public IEnumerable<TheaterItem> Theaters
        {
            get { return _theaters; }
            set
            {
                _theaters = value;
                RaisePropertyChanged("Theaters");
            }
        }

        private IEnumerable<TheaterItem> _theaters2;
        public IEnumerable<TheaterItem> Theaters2
        {
            get { return _theaters2; }
            set
            {
                _theaters2 = value;
                RaisePropertyChanged("Theaters2");
            }
        }

        IEnumerable<TheaterItem> _allTheater;
        public IEnumerable<TheaterItem> AllTheater
        {
            get { return _allTheater; }
            set
            {
                _allTheater = value;
                RaisePropertyChanged("AllTheater");
            }
        }
        #endregion

        #region Command 들...

        #region ListPicker 버그로 임시(?)수정

        public RelayCommand<TheaterItem> SetTheaterCommand { get; set; }
        public void SetTheaterCommandExecute(TheaterItem theater)
        {
            if (theater == null) return;
            theater.SetResource("SelectedTheater");
            selectedTheater = theater;
        }

        //public RelayCommand<Region> SetSubRegionCommand { get; set; }
        public void SetRegionCommandExecute(Region region)
        {
            if (region == null) return;
            Region2 = region.SubRegions;
        }

        //public RelayCommand<SubRegion> SetTheatersCommand { get; set; }
        public void SetSubRegionCommandExecute(SubRegion region)
        {
            if (region == null)
            {
                Theaters = null;
                return;
            }
            selectedRegion = region;
            Theaters = region.TheaterItems;
        }
        #endregion

        public RelayCommand<ListPicker> SetRegionCommand { get; set; }
        public void SetRegionCommandExecute(ListPicker listPicker)
        {
            Region region = listPicker.SelectedItem as Region;
            SetRegionCommandExecute(region);
        }

        public RelayCommand<ListPicker> SetSubRegionCommand { get; set; }
        public void SetSubRegionCommandExecute(ListPicker listPicker)
        {
            SubRegion region = listPicker.SelectedItem as SubRegion;
            SetSubRegionCommandExecute(region);
        }

        public RelayCommand<string> SearchTheaterCommand { get; set; }
        public void SearchTheaterCommandExecute(string keyword)
        {
            if (AllTheater == null) return;
            Theaters2 = AllTheater.Where(item => item.Name.ToLower().Contains(keyword.ToLower()));
        }
        #endregion

        #region 생성자

        public TheaterSearch2ViewModel()
        {
            //SetMovieCommand = new RelayCommand<MovieItem>(SetMovieCommandExecute);
            //SetSubRegionCommand = new RelayCommand<Region>(SetSubRegionCommandExecute);
            //SetTheatersCommand = new RelayCommand<SubRegion>(SetTheatersCommandExecute);
            //SetTheaterCommand = new RelayCommand<TheaterItem>(SetTheaterCommandExecute);
            SetRegionCommand = new RelayCommand<ListPicker>(SetRegionCommandExecute);
            SetSubRegionCommand = new RelayCommand<ListPicker>(SetSubRegionCommandExecute);
            SetTheaterCommand = new RelayCommand<TheaterItem>(SetTheaterCommandExecute);
            SearchTheaterCommand = new RelayCommand<string>(SearchTheaterCommandExecute);

            if (IsInDesignMode)
            {
                #region 디자인용 더미데이터
                Region1 = new ObservableCollection<Region> {
                    new Region { Title = "서울" },
                    new Region { Title = "경기도" },
                    new Region { Title = "충청도" },
                    new Region { Title = "광주/전라/제주" }
                };
                Region2 = new ObservableCollection<SubRegion> {
                    new SubRegion { Title = "목포" },
                    new SubRegion { Title = "광주" },
                    new SubRegion { Title = "순천" },
                    new SubRegion { Title = "장성" }
                };
                Theaters = new ObservableCollection<TheaterItem> {
                    new TheaterItem { Name = "CGV" },
                    new TheaterItem { Name = "프리머스" },
                    new TheaterItem { Name = "롯데시네마" },
                    new TheaterItem { Name = "메가박스" }
                };
                #endregion
            }
            else
            {
                #region 디자인용 더미데이터
                Region1 = new ObservableCollection<Region> {
                    new Region { Title = "서울" },
                    new Region { Title = "경기도" },
                    new Region { Title = "충청도" },
                    new Region { Title = "광주/전라/제주" }
                };
                Region2 = new ObservableCollection<SubRegion> {
                    new SubRegion { Title = "목포" },
                    new SubRegion { Title = "광주" },
                    new SubRegion { Title = "순천" },
                    new SubRegion { Title = "장성" }
                };
                Theaters = new ObservableCollection<TheaterItem> {
                    new TheaterItem { Name = "CGV" },
                    new TheaterItem { Name = "프리머스" },
                    new TheaterItem { Name = "롯데시네마" },
                    new TheaterItem { Name = "메가박스" }
                };
                AllTheater = new ObservableCollection<TheaterItem> {
                    new TheaterItem { Name = "CGV" },
                    new TheaterItem { Name = "프리머스" },
                    new TheaterItem { Name = "롯데시네마" },
                    new TheaterItem { Name = "메가박스" }
                };
                #endregion
                //getAllTicketingInfo();
            }
        }
        #endregion

        #region 전체 영화목록 및 극장목록 다운로드

        private void getAllTicketingInfo()
        {
            string url = "http://movie.naver.com/movie/reservation/ticketingInfo2.nhn";
            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(AllTicketingInfoDownloaded);
            client.DownloadStringAsync(new Uri(url));
        }
        void AllTicketingInfoDownloaded(object sender, DownloadStringCompletedEventArgs e)
        {
            XDocument xdoc = XDocument.Parse(e.Result);

            Region1 = xdoc.Root.Element("theaters").Descendants("region1")
                .Select(region1 => new Region {
                    Title = region1.Attribute("title").Value,
                    SubRegions = region1.Descendants("region2")
                    .Select( region2 => new SubRegion {
                        Title = region2.Attribute("title").Value,
                        TheaterItems = region2.Descendants("item")
                        .Select( item => new TheaterItem {
                                Key = item.Attribute("key").Value,
                                Name = item.Attribute("name").Value
                            })
                    })
                });

            AllTheater = xdoc.Root.Element("theaters").Descendants("item")
                .Select(item => new TheaterItem
                {
                    Key = item.Attribute("key").Value,
                    Name = item.Attribute("name").Value
                });
        }
        #endregion
    }
}